package com.ruoyi.produce.process.costAccounting.service;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.produce.process.costAccounting.domain.CostAccounting;
import com.ruoyi.produce.process.costAccounting.domain.CostAccountingCond;
import com.ruoyi.produce.process.costAccounting.mapper.CostAccountingMapper;
import com.ruoyi.produce.process.costApportionment.domain.CostApportionmentListDto;
import com.ruoyi.produce.process.costApportionment.mapper.CostApportionmentMapper;
import com.ruoyi.produce.process.planoutsource.domain.ProducePlanOutsource;
import com.ruoyi.produce.process.planoutsource.mapper.ProducePlanOutsourceMapper;
import com.ruoyi.utils.ID.IdUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 成本结算
 * 业务逻辑
 */
@Service
public class CostAccountServiceImpl implements ICostAccountingService{
    @Autowired
    private CostAccountingMapper costAccountingMapper;
    @Autowired
    private ProducePlanOutsourceMapper outsourceMapper;
    @Autowired
    private CostApportionmentMapper costApportionmentMapper;
    @Autowired
    private IdUtil idUtil;
    /**
     * 查找成本结算
     * @param cond
     * @return
     */
    @Override
    public List<CostAccounting> selectCostAccountingList(CostAccountingCond cond) {
        List<CostAccounting> costAccountings = costAccountingMapper.selectCostAccountingList(cond);
        for (CostAccounting item : costAccountings) {
            //处理委外费用
            double outsourcingExpenses = 0.000;
            List<ProducePlanOutsource> producePlanOutsources = outsourceMapper.selectProducePlanOutsourceByPlanSelfId(item.getPlanSelfId());
            if (producePlanOutsources.size() != 0) {
                // 有委外记录
                for (int i = 0; i < producePlanOutsources.size(); i++) {
                    outsourcingExpenses += producePlanOutsources.get(i).getPlanOutsourcePrice();
                }
                item.setOutsourcingExpenses(outsourcingExpenses);
            }
            //处理费用分摊
            double costApportionmentExpenses = 0.000;
            CostApportionmentListDto costApportionmentList = costApportionmentMapper.selectCostApportionmentListListByPlanSelfId(item.getPlanSelfId());
            if (StringUtils.isNotNull(costApportionmentList)) {
                // 有分摊记录
                costApportionmentExpenses = costApportionmentList.getLaborCost() + costApportionmentList.getAuxiliaryMaterialCost() + costApportionmentList.getOtherCost();
            }
            item.setCostApportionmentExpenses(costApportionmentExpenses);
            // 处理总成本
            item.setTotalCost(outsourcingExpenses + costApportionmentExpenses + item.getNewCost());
        }
        return costAccountings;
    }

    /**
     * 根据生产计划编号查询是否有结算记录
     * 存在true 不存在false
     * @param planSelfId
     * @return
     */
    @Override
    public boolean selectCostAccountingByPlanSelfId(String planSelfId) {
        return StringUtils.isNotNull(costAccountingMapper.selectCostAccountingByPlanSelfId(planSelfId));
    }

    /**
     * 新增成本结算
     * @param costAccounting
     * @return
     */
    @Override
    public int insertCostAccounting(CostAccounting costAccounting) {
        String costAccountingId = idUtil.createCostAccountingId();
        costAccounting.setCostAccountingId(costAccountingId);
        if (StringUtils.isNotNull(costAccountingMapper.selectCostAccountingById(costAccountingId))) {
            //存在
            return 0;
        }
        return costAccountingMapper.insertCostAccounting(costAccounting);
    }

    /**
     * 更新成本结算
     * @param costAccounting
     * @return
     */
    @Override
    public int updateCostAccounting(CostAccounting costAccounting) {
        if (!StringUtils.isNotNull(costAccountingMapper.selectCostAccountingById(costAccounting.getCostAccountingId()))) {
            //不存在
            return 0;
        }
        return costAccountingMapper.updateCostAccounting(costAccounting);
    }

    /**
     * 删除成本结算
     * 仅修改成本结算删除标志
     * @param costAccounting
     * @return
     */
    @Override
    public int updateCostAccountingDeleteFlag(CostAccounting costAccounting) {
        return costAccountingMapper.updateCostAccountingDeleteFlag(costAccounting);
    }
}
